﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.Utilities;
using NetDon.WebControls;
using SaleDiscount.Portal.Common;
using SaleDiscount.Portal.Service;

namespace SaleDiscount.Portal
{
    public partial class SaleDiscountManagement : SaleDiscountPageBase
    {
        SaleDiscountService saleDiscountService = new SaleDiscountService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsCallback)
            {
                if (Status == "Inactive")
                {
                    Response.Redirect("~/TokenManagement.aspx", true);
                }

            }

            btnCreate.Attributes.Add("onclick", "createSaleDiscountEdit('{0}');return false;");

            PageQueryAdapter1.PageSize = 100;
            if (!IsPostBack)
            {

                PageQueryAdapter1.PerformQuery();

            }
        }

        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            var criteria = new SaleDiscountCriteria
            {



            };

            e.Condition = criteria;
        }

        public void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();



        }
        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            e.RecordCount = saleDiscountService.GetSaleDiscountCount((SaleDiscountCriteria)e.Condition);
        }

        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {

            e.DataSource = saleDiscountService.SearchSaleDiscountByCondition((SaleDiscountCriteria)e.Condition, gridCustomerInfoList.SortExpression,
                                                      gridCustomerInfoList.SortDirection, e.PageSize, e.PageIndex);
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }

        protected void gridOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var entity = (e.Row.DataItem);

                var id = e.Row.Attributes["Id"] = Convert.ToString(DataBinder.Eval(entity, "Id"));

            
                var btnEdit = (ImageButton)e.Row.FindControl("btnEdit");
                if (btnEdit != null)
                {
                    btnEdit.OnClientClick = string.Format("modifySaleDiscountEdit('{0}');return false;", id);
                }
                var btnViewListings = (ImageButton)e.Row.FindControl("btnViewListings");
                if (btnViewListings != null)
                {
                    btnViewListings.OnClientClick = string.Format("modifySaleDiscountEdit('{0}');return false;", id);
                }
                
                var btnDelete = (ImageButton)e.Row.FindControl("btnDelete");
                if (btnDelete != null)
                {
                    btnDelete.OnClientClick = string.Format("deleteDemand('{0}');return false;", id);
                    if (this.GetStatus(DataBinder.Eval(entity, "StartDate"), DataBinder.Eval(entity, "StartDate")) == "Active")
                    {
                        btnDelete.Visible = false;
                    }
                }
            }
        }
        protected void btnDeleteDemand_Click(object sender, EventArgs e)
        {

            var customerId = fieldCustomerId.Value.ToInt32();

            bool result = saleDiscountService.RemoveSaleDiscount(customerId.Value);

            if (result)
            {
                PageQueryAdapter1.PerformQuery();
                Alert("Remove successfully!");
            }
            else
            {
                Alert("Failed to remove.");
            }
        }


        protected string GetShipping(object id)
        {
            var shipping = "--"; ;
            if (!MiscUtilities.IsNullOrEmpty(id))
            {
                shipping= Convert.ToBoolean(id.ToString()) ? "Free" : "--";
            }

            return shipping;

        }
        protected string GetDiscount(object discountType, object discount)
        {
            if (MiscUtilities.IsNullOrEmpty(discountType)) return string.Empty;
            if (MiscUtilities.IsNullOrEmpty(discount)) return string.Empty;

            if (Convert.ToInt16(discountType.ToString()) == (int)DiscountType.Fixed)
            {
                return "$" + (discount.ToString().ToDecimal() ?? 0).ToString("#,###.00");
            }
            else
            {
                return ((discount.ToString().ToDecimal() ?? 0)).ToString("#,###") + "%" + " Off";
            }
        }



    }
}